package com.entfrm.biz.it.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.entfrm.biz.it.entity.PropertyRecord;
import com.entfrm.biz.it.manager.PropertyRecordManager;
import com.entfrm.biz.it.service.PropertyRecordService;
import com.entfrm.core.base.constant.CommonConstants;
import com.entfrm.core.log.annotation.OperLog;
import org.springframework.security.access.prepost.PreAuthorize;
import com.entfrm.core.base.api.R;
import com.entfrm.core.base.util.ExcelUtil;
import lombok.AllArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import java.util.List;
import java.util.Arrays;

/**
 * @author entfrm
 * @date 2020-08-08 13:21:28
 * @description 资产使用记录Controller
 */
@Api("资产使用记录管理")
@RestController
@AllArgsConstructor
@RequestMapping("/it/propertyRecord")
public class PropertyRecordController {

    private final PropertyRecordService propertyRecordService;

    private final PropertyRecordManager propertyRecordManager;

    private QueryWrapper<PropertyRecord> getQueryWrapper(PropertyRecord propertyRecord) {
        return new QueryWrapper<PropertyRecord>()
                .like(StrUtil.isNotBlank(propertyRecord.getUsername()), "username", propertyRecord.getUsername())
                .eq(StrUtil.isNotBlank(propertyRecord.getJobId()), "job_id", propertyRecord.getJobId())
                .like(StrUtil.isNotBlank(propertyRecord.getDeptName()), "dept_name", propertyRecord.getDeptName())
                .eq(StrUtil.isNotBlank(propertyRecord.getUseStatus()), "use_status", propertyRecord.getUseStatus())
                .orderByAsc("create_time");
    }

    @ApiOperation("资产使用记录列表")
    @PreAuthorize("@ps.hasPerm('PropertyRecord_view')")
    @GetMapping("/list")
    public R list(Page page, PropertyRecord propertyRecord) {
        IPage<PropertyRecord> propertyRecordPage = propertyRecordService.page(page, getQueryWrapper(propertyRecord));
        return R.ok(propertyRecordPage.getRecords(), propertyRecordPage.getTotal());
    }

    @ApiOperation("根据propertyId查询资产使用记录列表")
    @GetMapping("/getPropertyRecords/{propertyId}")
    public R getPropertyRecords(@PathVariable("propertyId")Integer propertyId) {
        List<PropertyRecord> propertyRecordList = propertyRecordManager.getPropertyRecords(propertyId);
        return R.ok(propertyRecordList);
    }

    @ApiOperation("资产使用记录查询")
    @GetMapping("/{id}")
    public R getById(@PathVariable("id") Integer id) {
        return R.ok(propertyRecordService.getById(id));
    }

    @OperLog("资产使用记录新增")
    @ApiOperation("资产使用记录新增")
    @PreAuthorize("@ps.hasPerm('PropertyRecord_add')")
    @PostMapping("/save")
    public R save(@Validated @RequestBody PropertyRecord propertyRecord) {
        propertyRecordManager.savePropertyRecord(propertyRecord);
        return R.ok();
    }

    @OperLog("资产使用记录修改")
    @ApiOperation("资产使用记录修改")
    @PreAuthorize("@ps.hasPerm('PropertyRecord_edit')")
    @PutMapping("/update")
    public R update(@Validated @RequestBody PropertyRecord propertyRecord) {
        propertyRecordManager.updatePropertyRecord(propertyRecord);
        return R.ok();
    }

    @PreAuthorize("@ps.hasPerm('PropertyRecord_export')")
    @GetMapping("/export")
    public R export(PropertyRecord propertyRecord) {
        List<PropertyRecord> list = propertyRecordService.list(getQueryWrapper(propertyRecord));
        ExcelUtil<PropertyRecord> util = new ExcelUtil<PropertyRecord>(PropertyRecord.class);
        return util.exportExcel(list, "资产使用记录数据");
    }
}
